package com.flysh.host.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flysh.host.model.User;
import com.flysh.host.service.SystemAccessManagerService;
import com.flysh.host.service.SystemAccessManagerServiceImpl;
import com.flysh.host.service.UserManagerService;

public class UserServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		   String operate = request.getParameter("operate");
		   if(operate==null){
			   operate = "login";
		   }
		   if(operate.equals("login")){
			   login(request,response);
		   }else if(operate.equals("add")){
			   //addUser(request,response);
			   addListUser(request,response);
		   }
		
		
		   
         
	
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		   SystemAccessManagerService service = new SystemAccessManagerServiceImpl();
		   UserManagerService userService = service.getUserManagerService();
		   String username = request.getParameter("username");
		   String pwd = request.getParameter("password");
		   User user = new User();
		   user.setUsername(username);
		   user.setPassword(pwd);
		   User u = userService.login(user);
		   if(u!=null){
			   request.setAttribute("username", username);
			   request.getRequestDispatcher("/index.jsp").forward(request, response);
		   }else{
			   request.setAttribute("abc", "error");
			   request.getRequestDispatcher("/login.jsp").forward(request, response);
		   }
	}
	
	private void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		 SystemAccessManagerService service = new SystemAccessManagerServiceImpl();
		   UserManagerService userService = service.getUserManagerService();
		   User user = new User();
		   boolean flag = true;
		   String username =request.getParameter("username");
		   if(username!=null&&!username.equals("")){
			   user.setUsername(username);
		   }else{
			   flag = false;
		   }
		   String password =request.getParameter("password");
		   if(password!=null&&!password.equals("")){
			   user.setPassword(password);
		   }else{
			   flag = false;
		   }
		   String truename =request.getParameter("truename");
		   if(truename!=null&&!truename.equals("")){
			   user.setTruename(truename);
		   }else{
			   flag = false; 
		   }
		   String email =request.getParameter("email");
		   if(email!=null&&!email.equals("")){
			   user.setEmail(email);
		   }else{
			   flag = false;
		   }
		   
		   PrintWriter pw =response.getWriter();
		   if(flag){
			   int num = userService.add(user);
			   if(num>0){
					  pw.write("success");
				   }else{
					  pw.write("fail");
				   }
				 
		   }else{
			   pw.write("fail");
		   }
		   
		   pw.close();
		   
		   
		 
		
		
	}
	
	private void addListUser(HttpServletRequest request, HttpServletResponse response) throws IOException{
		SystemAccessManagerService service = new SystemAccessManagerServiceImpl();
		   UserManagerService userService = service.getUserManagerService();
		   List<User> list = new ArrayList<User>();
		  String str = request.getParameter("param");
		  String[] userStr =str.split(";");
		 for (String string : userStr) {
			 String[] sstr = string.split(",");
			 User user = new User();
			 for (int i = 0; i < sstr.length; i++) {
				 user.setUsername(sstr[0]);
				 user.setPassword(sstr[1]);
				 user.setTruename(sstr[2]);
				 user.setEmail(sstr[3]);
			}
			 list.add(user); 
		}
		 int sum=0;
		 for(User user: list){
			 int num = userService.add(user);
			 sum+=num;
		 }
		 
		   PrintWriter pw =response.getWriter();
		 
			   if(sum==list.size()){
					  pw.write("success");
				}else{
					  pw.write("fail");
				}
			   pw.close();
		
		   
		  
		 
	}
	

}
